package com.cskaoyan.javase.practice.stage3;

import java.util.Scanner;

/**
 * @program: little-practice
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-01 15:36
 **/

public class StudentListTool {

    static int stuIdCount = 1;

    private StudentListTool() {

    }

    public static Student getStuByScanner() {
        String name;
        String gender;
        int age;
        int grade;
        int tele;
        Scanner sc = new Scanner(System.in);
        System.out.println("换行输入姓名、性别、年龄、年纪、手机号：");
        name = sc.nextLine();
        gender = sc.nextLine();
        age = Integer.parseInt(sc.nextLine());
        grade = Integer.parseInt(sc.nextLine());
        tele = Integer.parseInt(sc.nextLine());
        return new Student(0, name, gender, age, grade, tele);
    }

    public static String getStuNameByScanner() {
        System.out.println("输入要操作的学生姓名： ");
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }

    public static void showStudentList(Student[] stu) {
        if (stu == null) {
            System.out.println("null");
            return;
        }
        boolean isEmpty = true;
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] == null)
                continue;
            stu[i].printStu();
            isEmpty = false;
        }
        if (isEmpty)
            System.out.println("数组为空");
    }

    public static void addStudentInfo(Student[] stu) {
        Student stuAdd = getStuByScanner();
        stuAdd.stuId = stuIdCount++;
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] == null) {
                stu[i] = stuAdd;
                return;
            }
        }
        System.out.println("数组已满！");
    }

    public static void deleteStudentInfo(Student[] stu) {
        String temp = getStuNameByScanner();
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null && temp.equals(stu[i].name)) {
                stu[i] = null;
                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("无此人，删除失败！");
    }

    public static void updateStudentInfo(Student[] stu) {
        String temp = getStuNameByScanner();
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null && temp.equals(stu[i].name)) {
                int stuId = stu[i].stuId;
                stu[i] = getStuByScanner();
                stu[i].stuId = stuId;
                return;
            }
        }
        System.out.println("无此人，更新失败！");
    }

    public static void searchStudentInfo(Student[] stu) {
        String temp = getStuNameByScanner();
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null && temp.equals(stu[i].name)) {
                System.out.println("学生 " + temp + " 下标为 " + i);
                return;
            }
        }
        System.out.println("无此人，查找失败！");
    }
}
